//
//  PAChekText.m
//  PAMobileWallet
//
//  Created by shen pu on 13-7-21.
//  Copyright (c) 2013年 平安付. All rights reserved.
//

#import "PAChekText.h"
#include <sys/xattr.h>

@implementation PAChekText

+ (StringStatus)validString:(NSString *)string {
    if (string == nil) {
        return Status_Nil;
    }
    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([string length] == 0) {
        return  Status_NULL;
    }
    return 1;
}

//手机验证
+ (StringStatus)validMobileNo:(NSString *)MobileNo{
    if ([self validString:MobileNo] < 0) {
        return [self validString:MobileNo];
    }
    
    NSString * mobileNumStr = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate * mobileNum = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",mobileNumStr];

    if (![mobileNum evaluateWithObject:MobileNo]) {
        return Status_MobileNo;
    }
    
    return 1;
}

//手机验证  全
+ (BOOL)isMobileNumber:(NSString *)mobileNum
{
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";;
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";;
    NSPredicate * regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",MOBILE];
    NSPredicate * regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CM];
    NSPredicate * regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CU];
    NSPredicate * regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CT];

    if([regextestmobile evaluateWithObject:mobileNum] || [regextestcm evaluateWithObject:mobileNum] || [regextestct evaluateWithObject:mobileNum] || [regextestcu evaluateWithObject:mobileNum])
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

// 验证银行卡  
+ (StringStatus)validBankCard:(NSString *)bankCard
{
    NSString * bankCardStr  = @"^[0-9]{15,19}$";
    NSPredicate * regextestBankCard = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",bankCardStr];
    
    if (![regextestBankCard evaluateWithObject:bankCard]){
        return Status_BankCode;
    }
    
    return 1;
}

// 验证验证码
+ (StringStatus)validVerificationCode:(NSString *)verificationCode
{
    NSString * verificationCodeStr  = @"^[0-9]{6}$";
    NSPredicate * regextestverificationCode = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",verificationCodeStr];
    
    if (![regextestverificationCode evaluateWithObject:verificationCode]){
        return Status_VerificationCode;
    }
    
    return 1;
}

//邮箱验证
+ (StringStatus)validEmail:(NSString *)Email{
    if ([self validString:Email] < 0) {
        return [self validString:Email];
    }
    
    NSString *regexEmailStr = @"\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b";
    NSPredicate *regexEmail = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexEmailStr];

    if (![regexEmail evaluateWithObject:Email]) {
        return Status_Email;
    }
    return 1;
}

//身份证验证 方法1
+ (StringStatus)validCertiNo:(NSString *)CertiNo{
    if ([self validString:CertiNo] < 0) {
        return [self validString:CertiNo];
    }
    
    NSString *regex15 = @"[1-9][0-9]{7}((0[0-9])|(1[0-2]))(([0|1|2][0-9])|3[0-1])[0-9]{3}";
    NSPredicate *regexTest15 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex15];
    
    NSString *regex18 = @"[1-9][0-9]{5}[1-9][0-9]{3}((0[0-9])|(1[0-2]))(([0|1|2][0-9])|3[0-1])[0-9]{3}([0-9]|X)";
    NSPredicate *regexTest18 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex18];
    
    if(![regexTest15 evaluateWithObject:CertiNo] || ![regexTest18 evaluateWithObject:CertiNo]){
        return Status_CertiNo;
    }
    
    return 1;
}

//身份证验证 方法2
+ (StringStatus)validCardID:(NSString *)getCardID{
    if ([self validString:getCardID] < 0) {
        return [self validString:getCardID];
    }
    
    NSString * regexCardIDStr = @"(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)";
    NSPredicate * regexCardID = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexCardIDStr];
    if(![regexCardID evaluateWithObject:getCardID]){
        return Status_CertiNo;
    }
    return 1;
}

// 真实姓名验证
+ (StringStatus)validUserName:(NSString *)username {
    if ([self validString:username] < 0) {
        return [self validString:username];
    }

    NSString *regex = @"[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+";
    NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];//中文名字
    NSString *regexEng = @"[A-Za-z ]+";
    NSPredicate *regexTestEng = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexEng];//英文名字
    
    if(![regexTest evaluateWithObject:username] || ![regexTestEng evaluateWithObject:username]){
        return Status_ValidName;
    }
    return 1;
}

// 验证设置 登录 密码  6-20个字母，数字，符号
+ (StringStatus)validSetLoginPW:(NSString *)setLoginPW {
    if ([self validString:setLoginPW] < 0) {
        return [self validString:setLoginPW];
    }

    //^[\x21-\x7E]{6,20}$ 也可以
    NSString * regexLoginPWStr = @"^[A-Za-z0-9_-]{6,20}$"; //符号支持_-两种
    NSPredicate * regexLoginPW = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexLoginPWStr];
    if (![regexLoginPW evaluateWithObject:setLoginPW]) {
        return Status_ValidLoginPW;
    }
    
    return 1;
}

// 验证设置 支付 密码
+ (StringStatus)validSetPayPW:(NSString *)setPayPW {
    if ([self validString:setPayPW] < 0) {
        return [self validString:setPayPW];
    }
    
    NSString * regexLoginPWStr = @"^[A-Za-z0-9_-]{6,20}$"; //符号支持_-两种
    NSPredicate * regexLoginPW = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexLoginPWStr];
    if (![regexLoginPW evaluateWithObject:setPayPW]) {
        return Status_ValidPayPW;
    }
    
    return 1;
}

//邮编验证
+ (StringStatus)validZipCode:(NSString *)ZipCode {
    if (![ZipCode isMatchedByRegex:@"[0-9]\\d{5}(?!\\d)"]) {
        return Status_ZipCode;
    }
    return 1;
}

+ (UIAlertView *)tipingAlert:(NSString *)getMessage isCancle:(NSString *)getTypeStr
{
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"登录密码和支付密码不能相同" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:[getTypeStr isEqualToString:@"取消"]?@"取消":nil, nil];
    [alert show];
    return alert;
}

@end
